import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import AddEditModal from '#cpt/temp/AddEditModal'
import TableControls from '#cpt/temp/TableControls'
import Controls from '#cpt/temp/Controls'
import Confirm from '#cpt/temp/Confirm'
// ===================================================================== global declare
const { $fn, $Lazy, $http } = window
// ===================================================================== Lazy
const Page 				= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 			= $Lazy.file(()=>import('#private/cache-api'))
const $time 			= $Lazy.file(()=>import('@com/utils/time'))
//
const StopReportModal 	= $Lazy.state(()=>import('#private/pages/stop-report-modal'))
const ReportDelModal	= $Lazy.state(()=>import('./cpt/report-del-modal'))
const AddReportModal	= $Lazy.state(()=>import('./cpt/add-report-modal'))
const MtimeModal		= $Lazy.state(()=>import('./cpt/mtime-modal'))

// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		selectedRows:[],
		// 搜索表单列表
		forms : [
			/* 接收时间放第一位 2021-08-26  --耿琰 */	// 生成时间放第一位 2021-9-8  --马婷 
			{ label: '生成时间',		names: ['start_date','end_date'], type:'date-range', name:'jz-time' },
			{ label: '编号',		name: 'spec_num' },
			{ label: '标本条码',		name: 'spec_code' },
			{ label: '状态',		name: 'status',		type: 'select', loading:true, idStr:'code' },
			{ label: '医院',		name: 'hosp_id',	type: 'select', loading:true },
			{ label: '姓名',		name: 'patient_name' },
			{ label: '第三方条码',	name: 'third_spec_code' },
			{ label: '报告单模板',	name: 'report_name' },
			{ label: '开始条码',		name: 'start_code' },
			{ label: '结束条码',		name: 'end_code' },
			{ label: '接收时间',		names: ['sp_start_at','sp_end_at'], name:'jz-time1', type:'date-range' },
			{ label: '采样时间',		names: ['sample_start_date','sample_end_date'], name:'jz-time2', type:'date-range' },
		],
		// 提交表单列表
		formData : [
			{ label: '报告单模板',		name: 'tmpl_id', 	half:true, type: 'select', required:true, onChange:({rows})=>{
				if(rows){
					// 每次切换时，清空模板版本号
					this.formRef.reset()
					// 更新模板版本号数据
					this.getTempVersion(rows.value)
				}
			} },
			{ label: '模板版本号',		name: 'tmpl_vs_id', 		half:true, type: 'select', required:true },
		]
	}
	
	model = {}
	
	componentDidMount(){
		this.getSelect()
		/* 报告单管理接收时间默认为1星期 2021-08-16  --耿琰 */
		$time.then(f=>{
			const arr = f.duration['7-0-24']()
			this.resetModel = { start_date: arr[0], end_date: arr[1], 'jz-time':arr }	 // 取消默认接收时间，生成时间默认一星期 2021-9-8 --马婷 
			this.model = $fn.copy(this.resetModel)
			// this.fetch()		 /* 报告单管理一进入不加载数据 2021-8-27 --马婷 */
		})
	}
	// 获取下拉数据
	getSelect = () => {
		const { forms } = this.state
		
		$cache.then( async f => {
			// 状态
			$fn.setRow(forms, 'status', await f.getDicCache({ code:90 } ) )
			// 医院
			$fn.setRow(forms, 'hosp_id', await f.getCache({ api:'hospital' } ) )
			this.setState({ forms })
		})
	}
	// 获取弹窗下拉
	getModalSelect = async () => {
		const { formData } = this.state
		$fn.setModalRows(formData, 'tmpl_id', ()=>{
			$cache.then( async f => {
				// 报告单模板
				$fn.setRow(formData, 'tmpl_id', await f.getCache({ api:'kd-report-from/select' } ) )
				this.setState({ formData })
			})
		})
	}
	
	// 获取模板版本号
	getTempVersion = async (report_tpl_id) =>{
		await $http.submit(null,'kd-report-from-version/select', { param: { report_tpl_id }, isPull:true } ).then(data=>{
			const { formData } = this.state
			$fn.setRow(formData, 'tmpl_vs_id', data.items)
			this.setState({ formData })
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'kd-report-spec/index')
	
	// 表单
	cols = [
		{ title: '编号',			dataIndex: 'spec_num', 			...$fn.table.spec_num },
		{ title: '标本条码', 		dataIndex: 'spec_code', 		...$fn.table.spec_code },
		{ title: '第三方条码', 		dataIndex: 'third_spec_code', 	width:150 },
		{ title: '姓名', 			dataIndex: 'patient_name', 		...$fn.table.name  },
		{ title: '版本号', 			dataIndex: 'tmpl_vs', 			width:50 },
		{ title: '报告单模板', 		dataIndex: 'report_name', 		width:200, render:(text, rows)=> text + ' — ' + rows.report_form },
		{ title: '所属医院', 		dataIndex: 'hosp_name', 		width:200 },
		{ title: '所属派送人', 		dataIndex: 'salesman', 			width:100 },
		{ title: '接收时间', 		dataIndex: 'check_time', 		...$fn.table.time },	 // 修改接收时间字段 2021-10-8  --马婷 
		{ title: '生成时间', 		dataIndex: 'created_at', 		...$fn.table.time },
		{ title: '派送完成时间', 	dataIndex: 'update_at', 		...$fn.table.time },
		{ title: '报告单状态', 		dataIndex: 'status_name', 		width:90 },
		TableControls.controls.call(this, { width: 250})
	]
	// 表格操作按钮
	table_controls = (text,{ uuid, spec_num, spec_code }) => {
		return [
			TableControls.edit.call(this, 'kd-report-spec/info', { uuid }, {
				select: this.getModalSelect,
				onValue: async ({ tmpl_id, tmpl_vs_id, uuid }) => {
					await this.getTempVersion(tmpl_id)
					this.formRef.setValue({tmpl_id, tmpl_vs_id})
				}
			}),				// 编辑
			TableControls.pushTo.call(this, '查看', `check-report?uuid=${uuid}`),
			// 新增快速修改基本资料功能 20210927 耿琰
			TableControls.pushTo.call(this, '修改基本资料', `change-info?uuid=${uuid}`),
			TableControls.pushTo.call(this,'预览', `preview-report?spec_num=${spec_num}&spec_code=${spec_code}`),
		]
	}
	user = $fn.getUser() ?? {}
	// 操作按钮
	controls = () => {
		const { selectedKeys, selectedRows } = this.state
		const spec_nums = []
		selectedRows.forEach(({spec_num}) => spec_nums.push(spec_num))
		return [
			// Controls.printer.call(this, { label:'打印', batch:true, code:50106 }),
			Controls.pushTo.call(this,'打印', `preview-report?spec_nums=${spec_nums.join(',')}`, { isKeys: 1 }),
			Confirm.call(this, '推送到查询平台', 'kd-report-spec/supplementrep', { isKeys: 1, refresh:false,
				param: (keys, rows)=>{
					return  ({ params: rows.map(({ spec_num, spec_code, status })=>({ spec_num, spec_code, status }))  })
				}
			 }),
			Controls.click.call(this, '补报告单', () => this.addReportModal.open()),
			/* 报告单管理新增修改时间按钮 2021-08-10  --耿琰 */
			// ['NS010123','NS010007','NS020057'].includes(this.user.empl_name) && Controls.click.call(this, '修改时间', () => this.mtimeModal.open()),
			Controls.click.call(this, '修改时间', () => this.mtimeModal.open(selectedRows)),
			Controls.click.call(this, '报告单删除', () => this.reportDelModal.open(selectedRows), { isKeys:1 }),
			Controls.click.call(this, '报告单终止', () => this.stopReportModal.open({ ids: selectedKeys, page_source: 'kf' }), { isKeys:1 }),
		]
	}
	render(){
		return (
			<>
				<Page title='报告单管理' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this,{ timeStr:'7-0-24' }) }
					{/* 表格 */}
					{ Table.call(this) }
				</Page>
				{/* 添加、编辑 */}
				{ AddEditModal.call(this,{ editApi:'kd-report-spec/edit' }) }
				{/* 删除报告单 */}
				<ReportDelModal onRef={ref=>this.reportDelModal = ref} onRefresh={this.fetch} onClear={$fn.clearKeys.bind(this)} />
				{/* 报告单终止 */}
				<StopReportModal onRef={ref=>this.stopReportModal = ref} onRefresh={this.fetch} api='kd-report-spec/abortReport' />
				{/* 补报告单 */}
				<AddReportModal onRef={ref=>this.addReportModal = ref} onRefresh={this.fetch}/>
				
				{/* 报告单管理新增修改时间按钮 2021-08-10  --耿琰 */}
				{/* 修改时间 */}
				<MtimeModal onRef={ref=>this.mtimeModal = ref} onRefresh={this.fetch}/>
			</>
		)
	}
}

export default Index